from machine import Pin, SoftI2C import dht import time from I2C_LCD import I2cLcd # ------------------------------ # DHT11 Setup - TRY DIFFERENT PINS # ------------------------------ # Common DHT11 pins: 4, 5, 15, 16, 17, 18, 19, 21, 22, 23 DHT_PIN = 18 print(f"DHT11 on GPIO {DHT_PIN}") dht_sensor = dht.DHT11(Pin(DHT_PIN)) # ------------------------------ # LCD Setup # ------------------------------ i2c_lcd = SoftI2C(scl=Pin(14), sda=Pin(13)) devices = i2c_lcd.scan() if devices: lcd_addr = devices[0] lcd = I2cLcd(i2c_lcd, lcd_addr, 2, 16) lcd.clear() lcd.putstr("DHT11 Ready") time.sleep(1) print("LCD initialized") else: lcd = None print("No LCD found") # ------------------------------ # Main Loop # ------------------------------ def main(): print("Starting DHT11 readings...") print("If you get timeouts, check:") print(" 1. DHT11 VCC connected to 3.3V") print(" 2. DHT11 GND connected to GND") print(" 3. DHT11 DATA connected to correct GPIO") print(" 4. Try different pin numbers (common: 4,5,15,16,17)") print() error_count = 0 success_count = 0 while True: try: # Read sensor dht_sensor.measure() temp = dht_sensor.temperature() humidity = dht_sensor.humidity() success_count += 1 error_count = 0 # Reset error counter on success # Display on Thonny console print(f"✓ Temp: {temp}°C | Humidity: {humidity}% | Success: {success_count}") # Display on LCD if lcd: lcd.clear() lcd.putstr(f"T:{temp}C H:{humidity}%") lcd.move_to(0, 1) lcd.putstr(f"OK: {success_count}") time.sleep(2) except OSError as e: error_count += 1 print(f"✗ Sensor read failed ({error_count}): {e}") if lcd: lcd.clear() lcd.putstr(f"Error: {error_count}") lcd.move_to(0, 1) lcd.putstr(f"Check GPIO {DHT_PIN}") # Stop after too many errors if error_count >= 5: print("\n❌ Too many errors. Please check:") print(f" - Is DHT11 data pin connected to GPIO {DHT_PIN}?") print(" - Is DHT11 powered (3.3V to VCC, GND to GND)?") print(" - Try changing DHT_PIN to a different GPIO") print("\nStopping...") if lcd: lcd.clear() lcd.putstr("Check wiring!") break time.sleep(2) except KeyboardInterrupt: print("\nStopped by user") break if __name__ == "__main__": main()